// 人事管理-->兼职教师课时薪水
Ext.onReady(function(){
    Ext.define('json_data',{
        extend: 'Ext.data.Model',
        fields: [
			 {name:'id',type:'string'},
             {name:'employe_id',type:'string'},
             {name:'employe__name',type:'string'},
             {name:'x1',type:'string'},
             {name:'x2',type:'string'},
             {name:'x3',type:'string'},
             {name:'x4',type:'string'},
             {name:'x5',type:'string'},
             {name:'x6',type:'string'},
             {name:'c1',type:'string'},
             {name:'c2',type:'string'},
             {name:'c3',type:'string'},
             {name:'g1',type:'string'},
             {name:'g2',type:'string'},
             {name:'g3',type:'string'}
           
        ]
    });
    //创建数据源
    var store = Ext.create('Ext.data.Store', {
    	pageSize:20,
        model: 'json_data',
	    proxy: {
	        type: 'ajax',
	        url: '/employecontrol/employejzteachersalary_list/',
	        actionMethods:'post',
	        reader: {
	            type: 'json',
	            root: 'rows',
	            totalProperty: 'totalCount'
	        }
	    },
	    autoLoad: true
    });
      // 员工-兼职教师combox
    var teacherQueryFormComboBox= Ext.create('Ext.form.field.ComboBox', { 
    	id : 'query_employe_id',
    	name:'query_employe_id',
    	fieldLabel: '兼职教师', 
    	emptyText: '请选择兼职教师',
    	typeAhead:true,
        minChars:1,
      	labelWidth: 60,
	    width : 190,
    	editable:true,
    	allowBlank : true,
    	store: new Ext.data.Store({ 
    		singleton : true, 
    		proxy: {
	    		type: 'ajax',
	    		url: '/employecontrol/employe_select29_5/',
	    		actionMethods:'post',
	    		reader: {
	    			type: 'json'
    			}
    	},
    	fields:['id', 'name'],
    	autoLoad:true 
    	}), 
    	loadingText:'正在加载数据，请稍侯……', 
    	triggerAction:'all', 
    	valueField:'id', 
    	displayField:'name',
    	listeners: {
    		'select': function(combo, records, opts) {	
   	    		
    		},
    		'beforequery' : function(queryEvent,eOpts){
				queryEvent.combo.getStore().load();
			} 

    	}
    });
    
    // 员工-兼职教师combox
    var teacherFormComboBox= Ext.create('Ext.form.field.ComboBox', { 
    	id : 'employe_id',
    	name:'employe_id',
    	fieldLabel: '兼职教师', 
    	emptyText: '请选择兼职教师',
    	typeAhead:true,
        minChars:1,
//      labelWidth: 60,
//	    width : 150,
    	editable:true,
    	allowBlank : false,
    	store: new Ext.data.Store({ 
    		singleton : true, 
    		proxy: {
	    		type: 'ajax',
	    		url: '/employecontrol/employe_select29_5/',
	    		actionMethods:'post',
	    		reader: {
	    			type: 'json'
    			}
    	},
    	fields:['id', 'name'],
    	autoLoad:true 
    	}), 
    	loadingText:'正在加载数据，请稍侯……', 
    	triggerAction:'all', 
    	valueField:'id', 
    	displayField:'name',
    	listeners: {
    		'select': function(combo, records, opts) {	
   	    		
    		},
    		'beforequery' : function(queryEvent,eOpts){
				queryEvent.combo.getStore().load();
			} 

    	}
    });
    
    //多选
    var selModel = Ext.create('Ext.selection.CheckboxModel');
    //创建Grid
    var grid = Ext.create('Ext.grid.Panel',{
        store: store,
        //height: 500,
        //selModel:selModel,
        columnLines:true,
        columns: [
            Ext.create('Ext.grid.RowNumberer'),// 序列
			{text: "ID", width: 80, dataIndex: 'id', sortable: false,hidden : true},
            {text: "兼职教师ID", width: 80, dataIndex: 'employe_id', sortable: false,hidden : true},
            {text: "兼职教师", width: 80, dataIndex: 'employe__name', sortable: false},
            {text: "小一", width: 60, dataIndex: 'x1', sortable: false},
            {text: "小二", width: 60, dataIndex: 'x2', sortable: false},
            {text: "小三", width: 60, dataIndex: 'x3', sortable: false},
            {text: "小四", width: 60, dataIndex: 'x4', sortable: false},
            {text: "小五", width: 60, dataIndex: 'x5', sortable: false},
            {text: "小六", width: 60, dataIndex: 'x6', sortable: false},
            {text: "初一", width: 60, dataIndex: 'c1', sortable: false},
            {text: "初二", width: 60, dataIndex: 'c2', sortable: false},
            {text: "初三", width: 60, dataIndex: 'c3', sortable: false},
            {text: "高一", width: 60, dataIndex: 'g1', sortable: false},
            {text: "高二", width: 60, dataIndex: 'g2', sortable: false},
            {text: "高三", width: 60, dataIndex: 'g3', sortable: false},
            
        ],
        // disableSelection: false,
        layout : 'fit',
        //frame:true,
        loadMask:true,
        region: 'north',
        //renderTo: 'notice-grid',
        viewConfig: {
            stripeRows: true,
            listeners: {
                itemclick: function(dataview, record, item, index, e) {
    				
                },
                itemdblclick: function(dataview, record, item, index, e) {
                	var selected = grid.getSelectionModel().getSelection();
        			if(selected!=''){
	        			form.getForm().reset();
	        			var selected = grid.getSelectionModel().getSelection();
						form.loadRecord(selected[0]);
	        			win.show();
        			}else{
        				Ext.Msg.alert('系统提示', '请选择需要修改的兼职教师课时薪水!');
        			}
                }
            }
        },
        dockedItems : [{
            xtype : 'pagingtoolbar',
            store : store,
            dock : 'bottom',
            displayInfo : true,
            //displayMsg: '显示 {0} - {1} 条，共计 {2} 条',
            autoScroll:true 
        },{
        	dock:'top',
        	xtype:'toolbar',
        	items:[teacherQueryFormComboBox,{
        		xtype:'button',
        		text:'查询',
        		iconCls:'search', 
        		handler:function(){
        			var params = { employe_id: Ext.getCmp('query_employe_id').getValue() };
        		    Ext.apply(store.proxy.extraParams, params);
        		    store.currentPage=1;
        		    store.load();
        		}
        	},{
        		xtype:'button',
        		text:'添加',
        		iconCls:'add', 
        		handler:function(){
        			form.getForm().reset();
        			win.show();
        		}
        	},{
        		xtype:'button',
        		text:'修改',
        		iconCls:'edit', 
        		handler:function(){
        			var selected = grid.getSelectionModel().getSelection();
        			if(selected!=''){
	        			form.getForm().reset();
	        			var selected = grid.getSelectionModel().getSelection();
						form.loadRecord(selected[0]);
	        			win.show();
        			}else{
        				Ext.Msg.alert('系统提示', '请选择需要修改的兼职教师课时薪水!');
        			}
        		}
        	},{
        		xtype:'button',
        		text:'删除',
        		iconCls:'delete', 
        		handler:function(){
        			var selected = grid.getSelectionModel().getSelection();
        			if(selected!=''){
        				Ext.MessageBox.confirm('提示', '确认删除吗?', function(btn){
	        				if(btn=='yes'){
			        			//var ids = [];
			        			var ids="";
			        			Ext.each(selected, function (item) {
			        				//ids.push(item.data.id);
			        				ids+=item.data.id+",";
			        			});
			        			Ext.Ajax.request({
			        			    url:'/employecontrol/employejzteachersalary_del/',
			        			    method : 'post',
			        			    params:{
			        			    	ids:ids
			        			    },
			        			    success:function(response){
			        			        var results =  Ext.decode(response.responseText);
			        			        if(results.success){
			        			        	store.load();
			        			        }else{
			        			        	Ext.Msg.alert('系统提示', results.message);
			        			        }
			        			    }
			        			});
	        				}});
        			}else{
        				Ext.Msg.alert('系统提示', '请选择需要删除的兼职教师课时薪水!');
        			}
        			
        		}
        	}]
        }]
    });
    

	var form = Ext.create('Ext.form.Panel', {
		//title: '电子公告信息',
		width: 300,
		//frame: true,
		bodyPadding: 5,
        fieldDefaults: {
            labelAlign: 'right',
            labelWidth: 90,
            msgTarget: 'side'
        },
        layout: 'anchor',
        defaults: {
            anchor: '100%'
        },
		items : [{
				xtype:'hiddenfield',
				fieldLabel:'id',
				name:'id'
			},teacherFormComboBox,{
				xtype:'numberfield',
				fieldLabel:'小一',
				allowBlank : false,
				value:'0',
				name:'x1'
			},{
				xtype:'numberfield',
				fieldLabel:'小二',
				allowBlank : false,
				value:'0',
				name:'x2'
			},{
				xtype:'numberfield',
				fieldLabel:'小三',
				allowBlank : false,
				value:'0',
				name:'x3'
			},{
				xtype:'numberfield',
				fieldLabel:'小四',
				allowBlank : false,
				value:'0',
				name:'x4'
			},{
				xtype:'numberfield',
				fieldLabel:'小五',
				allowBlank : false,
				value:'0',
				name:'x5'
			},{
				xtype:'numberfield',
				fieldLabel:'小六',
				allowBlank : false,
				value:'0',
				name:'x6'
			},{
				xtype:'numberfield',
				fieldLabel:'初一',
				allowBlank : false,
				value:'0',
				name:'c1'
			},{
				xtype:'numberfield',
				fieldLabel:'初二',
				allowBlank : false,
				value:'0',
				name:'c2'
			},{
				xtype:'numberfield',
				fieldLabel:'初三',
				allowBlank : false,
				value:'0',
				name:'c3'
			},{
				xtype:'numberfield',
				fieldLabel:'高一',
				allowBlank : false,
				value:'0',
				name:'g1'
			},{
				xtype:'numberfield',
				fieldLabel:'高二',
				allowBlank : false,
				value:'0',
				name:'g2'
			},{
				xtype:'numberfield',
				fieldLabel:'高三',
				allowBlank : false,
				value:'0',
				name:'g3'
			}]
	});

	var win = Ext.create('Ext.window.Window', {
		//title : '---',
		modal: true,
		closeAction: 'hide',
		items : [form],
		plain : true,
		resizable : false,
		buttonAlign : 'center',
		buttons : [ {
			text : '保存',
			handler : function() {
				if (form.getForm().isValid()) {
					form.getForm().submit({
						waitTitle : '请稍候',
						waitMsg : '正在处理......',
						url : '/employecontrol/employejzteachersalary_save/',
						success : function(form, action) {
							//Ext.Msg.alert('系统提示',  action.result.message);
							//form.getForm().reset();
							win.hide();
							store.load();
						},
						failure : function(form, action) {
							if (action.result) {
								Ext.Msg.alert('系统提示', action.result.message);
							} 
						}
					});
				}
			}
		}, {
			text : '取消',
			handler : function() {
				form.getForm().reset();
				win.hide();
			}
		} ]
	});    
    
	Ext.create('Ext.container.Viewport',{
		layout:'fit',
		renderTo: 'jzteachersalary-grid',
		items:[grid]
	});
	
});